var baseimg=null;

var sendQrcode = {
    init: function () {
        $(".payee").text(getQueryString("user"));
        s = new wxee();
        s.signature();
    },
    eventLisenter: function () {
        $(".graphic").click(function(){
            wx.chooseImage({
                count: 1, // 默认9
                success: function (res) {
                    var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                    wx.getLocalImgData({
                        localId:localIds[0], // 图片的localID
                        success: function (res) {
                            if(sendQrcode.isAndroid()){
                                baseimg = 'data:image/png;base64,'+res.localData;
                            }else{
                                baseimg = res.localData.replace('jgp', 'png');
                            }
                            $(".dd").css("display", "none");
                            $(".iiii").remove();
                            var dom = `<img class="iiii" style="max-width:100%;max-height:100%;" src="${baseimg}"/>`;
                            $(".graphic").append(dom);
                            $(".ddd").css("background","linear-gradient(90deg, #cba66a, #e0c794)");
                            $(".ddd").css("pointer-events","auto");
                        }
                    });
                }
            });
        });
        $(".ddd").click(function(){
            sendQrcode.uploadQrcode(baseimg);
        });
    },
    isAndroid:function (){
        var u = navigator.userAgent;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        // var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if(isAndroid){
            return true;
        }
        return false;
    },
    uploadQrcode:function(baseimg){
        new AjaxRequest({
            type: "post",
            url: apiService.uploadPayQrcode,
            param: JSON.stringify({data:baseimg}),
            callBack: function (res) {
                alert(res.message);
                if(res.success){
                    window.history.back();
                }
            },
            failBack: function () {
            }
        });
    }
}
//正则获取url参数
function getQueryString(name) {
    var search = document.location.search;
    var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
    var matcher = pattern.exec(search);
    var items = null;
    if (null != matcher) {
        try {
            items = decodeURIComponent(decodeURIComponent(matcher[1]));
        } catch (e) {
            try {
                items = decodeURIComponent(matcher[1]);
            } catch (e) {
                items = matcher[1];
            }
        }
    }
    return items;
}